package com.banshuibao.bsb_android.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.MyApplication;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.UserMessageModel;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.StatusBarUtils;
import com.bugtags.library.Bugtags;
import com.cunoraz.gifview.library.GifView;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by wangpan on 2017/11/29.
 */

public class BaseActivity extends AppCompatActivity {

    // 得到application对象
    public MyApplication applicationContext = MyApplication.applicationInstance;

    public View back_iv;

    public Activity context;

    private boolean isWindowsTranslucent;

    private View status_bar;

    private Dialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        isWindowsTranslucent = true;
        if (!StatusBarUtils.setWindowsTranslucent(this, context)) {
            isWindowsTranslucent = false;
            //StatusBarUtils.setWindowStatusBarColor(this, getResources().getColor(R.color.user_icon_stroke_blue1));
        }
        //禁止横竖屏切换，保持竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //添加到集合中
        applicationContext.addActivity(this);
        //页面有back键的点击后结束页面
//        initBack();
    //    getUserPost();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!isWindowsTranslucent && status_bar == null) {
            status_bar = findViewById(R.id.system_status_bar);
            if (status_bar != null)
                status_bar.setVisibility(View.GONE);
        }
        initBack();
    }

    /**
     * 设置页面标题
     **/
    public void setTitleText(String title) {
        if (findViewById(R.id.title_text) != null)
            ((TextView) findViewById(R.id.title_text)).setText(title);
    }

    /**
     * 页面有back键的点击后结束页面
     **/
    public void initBack() {
        if (back_iv == null) {
            back_iv = findViewById(R.id.back_iv);
            if (back_iv != null)
                back_iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        }
    }

    public void showProgressDialog() {
        if (progressDialog==null) {
            GifView gifView = (GifView) LayoutInflater.from(context).inflate(R.layout.dialog_progress_view, null);
            progressDialog = new Dialog(context, R.style.Translucent_Dialog_Theme);
            progressDialog.setContentView(gifView);
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    public void cancelProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing())
            progressDialog.dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁当前activity
        applicationContext.removeActivity(this);
        cancelProgressDialog();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //注：回调 1
        Bugtags.onResume(this);
        //友盟session的统计初始化
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //注：回调 2
        Bugtags.onPause(this);
        //友盟session的统计初始化
        MobclickAgent.onPause(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //注：回调 3
        Bugtags.onDispatchTouchEvent(this, event);
        return super.dispatchTouchEvent(event);
    }

    //读取用户信息得到申请状态
    private void getUserPost() {
        HttpUtil.doPost(Constants.Url.USER_MESSAGE, null, new HttpResponse(context, UserMessageModel.class) {
            @Override
            public void onResult(Object obj) {
                UserMessageModel messageModel = (UserMessageModel) obj;
              String status = messageModel.getData().getApplyStatus();
                if (status == null) {
                } else if (status.equals("0")) {
                    if (isRole()) {
                        IntentManager.goLoginActivity(context);
                        return;
                    }
                } else if (status.equals("1")) {
                    if (!isRole()) {
                        IntentManager.goLoginActivity(context);
                        return;
                    }
                } else if (status.equals("2")) {
                    if (isRole()) {
                        IntentManager.goLoginActivity(context);
                        return;
                    }
                }
            }
        });
    }
    /**
     * 判断用户身份是否是咨询师
     */
    private boolean isRole() {
        return TextUtils.equals("4", MyApplication.applicationInstance.getLoginInfo().getRole());
    }
}
